﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.SaleManage;
using YiSha.Model.Param.SaleManage;
using YiSha.Service.SaleManage;
using YiSha.Enum;

namespace YiSha.Business.SaleManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2019-12-04 22:06
    /// 描 述：产品业务类
    /// </summary>
    public class ProductBLL
    {
        private ProductService productService = new ProductService();
        private ProductTypeService productTypeService = new ProductTypeService();


        #region 获取数据
        public async Task<TData<List<ProductEntity>>> GetList(ProductListParam param)
        {
            TData<List<ProductEntity>> obj = new TData<List<ProductEntity>>();
            obj.Result = await productService.GetList(param);
            obj.TotalCount = obj.Result.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ProductEntity>>> GetPageList(ProductListParam param, Pagination pagination)
        {
            TData<List<ProductEntity>> obj = new TData<List<ProductEntity>>();
            obj.Result = await productService.GetPageList(param, pagination);
           // List<ProductTypeEntity> productTypeList = await productTypeService.GetList(new ProductTypeListParam { IsValid = StatusEnum.Yes.ParseToInt() });
            List<ProductTypeEntity> productTypeList = await productTypeService.GetList(null);

            foreach (var item in obj.Result)
            {
                item.ProductTypeName = productTypeList.Where(s => s.Id==item.ProductType).Select(s => s.ProductTypeName).FirstOrDefault();
            }
            obj.TotalCount = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<ProductEntity>> GetEntity(long id)
        {
            TData<ProductEntity> obj = new TData<ProductEntity>();
            obj.Result = await productService.GetEntity(id);
            if (obj.Result != null)
            {
                //if (obj.Result.ProductType > 0)
                //{
                //    ProductTypeEntity productTypeEntity = await productTypeService.GetEntity(obj.Result.ProductType.Value);
                //    if (productTypeEntity != null)
                //    {
                //        obj.Result.ProductTypeName = productTypeEntity.ProductTypeName;
                //    }
                //}
                obj.Result.ProductTypeId = obj.Result.ProductType.ToString();
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(ProductEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (!entity.ProductType.HasValue)
            {
                entity.ProductType = entity.ProductTypeId.ParseToLong();

            }
            await productService.SaveForm(entity);
            obj.Result = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await productService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
